<?php

require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'connection.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_date.php';


class dao_document {

    //Methods
    public function __construct() {

    }

    public function get_all() {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT dc.cat_name AS doc_cat_name,d.* FROM tbl_document d LEFT JOIN tbl_document_category dc ON d.doc_cat_id = dc.cat_id";
        /** @noinspection PhpParamsInspection */
        $result = mysqli_query($con, $query)
        or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();
        return $list;
    }

    public function save($doc_id, $doc_name, $doc_detail, $doc_cat_id,
            $doc_price, $doc_img_url, $doc_file) {
        $db = new connection();
        $con = $db->open_connect();

        if ($doc_id == 0) {
            $query = "INSERT INTO tbl_document(doc_name, doc_detail, doc_cat_id,
            doc_price, doc_img_url, doc_file, doc_added_date) VALUES (
                '" . $doc_name . "','" . $doc_detail . "'," . $doc_cat_id . "," . $doc_price . ",'" . $doc_img_url . "','" . $doc_file . "','" . lib_date::get_now() . "')";
        } else {
            $query = "UPDATE tbl_document SET "
                    . "doc_name = '" . $doc_name . "',"
                    . "doc_detail = '" . $doc_detail . "',"
                    . "doc_cat_id = " . $doc_cat_id . ","
                    . "doc_price = " . $doc_price . ","
                    . "doc_img_url = '" . $doc_img_url . "',"
                    . "doc_added_date = '" . lib_date::get_now() . "',"
                    . "doc_file = '" . $doc_file . "' "
                    . "WHERE doc_id = " . $doc_id;
        }
        /** @noinspection PhpParamsInspection */
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }

    public function get_by_id($id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT dc.cat_name AS doc_cat_name,d.* FROM tbl_document d LEFT JOIN tbl_document_category dc ON d.doc_cat_id = dc.cat_id WHERE doc_id = " . $id;
        /** @noinspection PhpParamsInspection */
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect();
        return $row;
    }

    public function delete($id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "DELETE FROM tbl_document WHERE doc_id = " . $id;
        /** @noinspection PhpParamsInspection */
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }

    public function count_document_by_category_id($category_id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_document WHERE doc_cat_id = " . $category_id;
        /** @noinspection PhpParamsInspection */
        $result = mysqli_query($con, $query)
        or die("Query fail: " . mysqli_error());
        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();
        return count($list);
    }

    public function count_all() {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_document";
        /** @noinspection PhpParamsInspection */
        $result = mysqli_query($con, $query)
        or die("Query fail: " . mysqli_error());
        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();
        return count($list);
    }

    public function get_all_documents_by_category_id($category_id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_document WHERE doc_cat_id = " . $category_id;
        /** @noinspection PhpParamsInspection */
        $result = mysqli_query($con, $query)
        or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();
        return $list;
    }
}
